import router from "@/router";
import { notification,message } from 'ant-design-vue';
import axios from 'axios';
import type { AxiosRequestConfig } from 'axios';
import { API_PREFIX, IS_USE_MOCK, RESPONSE_TYPE } from "../../config/constant";
import { getToken, removeToken } from "./auth";
// import {Ref} from "vue"
// import {partial} from "lodash"
// 接口定义返回的数据 返回的数据 多一项或少一项都将报错
export declare interface responseData<T> {
  code: number;
  msg: string;
  data: T;
}

export declare interface ErrorData<T = any> {
  code: number;
  msg: string;
  data: T;
}



// window.config.baseURL API_PREFIX : window.config.baseUrl
// 生产环境用
const BASE_URL =import.meta.env.MODE === 'production' ?   window.config.baseUrl : API_PREFIX

const instance = axios.create({
  baseURL: BASE_URL,
  withCredentials: true,
  timeout: 5000
})

// 请求拦截器
instance.interceptors.request.use(
  (config:AxiosRequestConfig) => {
    const token = getToken();
    if (token) {
      config.headers = {
        ...config.headers,
        Authorization: `bearer ${token}` ,
      }
    }

    (config.headers as any).apikey = '123456';
    // debugger
    return config
  },
  error => {
    console.log('请求拦截器报错', error)
    return Promise.resolve(error);
  }
)

// 响应拦截器
instance.interceptors.response.use(
  (response) => {
    const res = response.data as responseData<any>
    // 返回200正常返回
    if (res.code === RESPONSE_TYPE.success || IS_USE_MOCK ) {
      return res || true
    }
    if(res.code === 403){
      router.replace('/403');
      return
    }
    // 登录失效
    if (res.code === RESPONSE_TYPE.fail) {
      removeToken()
      router.replace('/login');
      return
    }
    if (res.code === RESPONSE_TYPE.error) {
      // 弹出错误信息
      // router.push({ name: '500', params: { message: res.msg || '出现错误' } })
      message.error(res.msg || '出现错误')
      return
    }
    if (res.code === 501) {
      return res || true
    }
  },
  error => {

    notification.open({
      message: 'Tips',
      description:error+'',
    });
  }
)

// 设置请求方法

// function requset<T = any>(config: AxiosRequestConfig): Promise<responseData<T>>

// function requset<T = any>(config: AxiosRequestConfig,useCancel:boolean): RequestResponse<T>

function requset <T = any>(config: AxiosRequestConfig,options?:AxiosRequestConfig): Promise<responseData<T>> {
  if(!options){
    return instance.request({
      ...config,
    })
  }

  return instance.request({
    ...config,
    cancelToken:options.cancelToken
  })
}

// partial

// export function get<T = any>(config: AxiosRequestConfig): Promise<responseData<T>> {
//   return requset<responseData<T>>({ ...config, method: 'GET' });
// }

// export function post<T = any>(config: AxiosRequestConfig): Promise<responseData<T>> {

//   return requset<responseData<T>>({ ...config, method: 'POST' })
// }

// export function put<T = any>(config: AxiosRequestConfig): Promise<responseData<T>> {

//   return requset<responseData<T>>({ ...config, method: 'PUT' })
// }

// export function del<T = any>(config: AxiosRequestConfig): Promise<responseData<T>> {

//   return requset<responseData<T>>({ ...config, method: 'DELETE' })
// }

export default requset;

export interface requestType<T>{
  (config: AxiosRequestConfig,options?:AxiosRequestConfig): Promise<responseData<T>>
}

// export interface requestCancelType<T> {
//   (config: AxiosRequestConfig,useCancel:boolean): RequestResponse<T>;
// }

// export interface RequestResponse<T> {
//   instance: Promise<responseData<T>>;
//   cancel: Ref<Canceler>;
// }



